package cn.nfwx.tv.fragment.dialog;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import cn.nfwx.tv.R;
import cn.nfwx.tv.base.BaseDialogFragment;
import cn.nfwx.tv.util.ScreenUtil;

public class WebViewDialogFragment extends BaseDialogFragment {
    protected static final String EXTRA_NAME_CANCELABLE = "cancelable";
    protected static final String EXTRA_NAME_URL = "url";
    private WebView webview_agreenment;

    public static WebViewDialogFragment newInstance(boolean cancelable) {
        WebViewDialogFragment instance = new WebViewDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_NAME_CANCELABLE, cancelable);
        instance.setArguments(bundle);
        return instance;
    }

    public static WebViewDialogFragment newInstance(String url) {
        WebViewDialogFragment instance = new WebViewDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_NAME_CANCELABLE, true);
        bundle.putString(EXTRA_NAME_URL, url);
        instance.setArguments(bundle);
        return instance;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_agreement;
    }

    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        webview_agreenment = findView(R.id.webview_agreenment);
        webview_agreenment.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        init();
    }

    private void init() {
        //是否允许关闭
        String url = getArguments().getString(EXTRA_NAME_URL);
        boolean cancelable = getArguments().getBoolean(EXTRA_NAME_CANCELABLE, true);
        setCancelable(cancelable);
        webview_agreenment.loadUrl(url);
    }


    @Override
    protected boolean isOutsideTouchable() {
        return true;
    }

    @Override
    protected boolean isTranslucentBackground() {
        return false;
    }

    @Override
    protected int getWidth() {
        return (int) (ScreenUtil.getDisplayWidth(getContext()) * 0.5);
    }

    @Override
    protected int getHeight() {
        return (int) (ScreenUtil.getDisplayHeight(getContext()) * 0.8);
    }
}
